package quq.missq.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.toolbox.NetworkImageView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

import quq.missq.Constants;
import quq.missq.R;
import quq.missq.activity.LoginActivity;
import quq.missq.activity.TaPageActivity;
import quq.missq.activity.TopicDetailActivity;
import quq.missq.beans.TopicBean;
import quq.missq.beans.UserBean;
import quq.missq.utils.UserTools;
import quq.missq.utils.VolleyTool;
import quq.missq.views.NoScrollGridView;

/**
 * Created by River on 7/27/15.
 */
public class TopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

    private static final int TYPE_FOOTER = 0;
    private static final int TYPE_TOPIC = 1;

    private Context mContext;
    private LinkedList<TopicBean.TopicResult> results;
    private LayoutInflater mInflater;

    public TopicAdapter(Context context){
        this.mContext = context;
        mInflater = LayoutInflater.from(context);
    }

    public void setData(LinkedList<TopicBean.TopicResult> results){
        this.results = results;
    }

    public LinkedList<TopicBean.TopicResult> getData(){
        return this.results;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        if(viewType == TYPE_FOOTER){
            View view = mInflater.inflate(R.layout.foot_layout,null);
            return new FootViewHolder(view);
        }else if(viewType == TYPE_TOPIC){
            View view = mInflater.inflate(R.layout.topic_adapter,null);
            return new TopicHolder(view);
        }else{
            return null;
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        if (viewHolder instanceof TopicHolder){
            final TopicHolder holder = (TopicHolder)viewHolder;
            final TopicBean.TopicResult result = results.get(position);
            holder.avtar.setImageUrl(Constants.IMAGE_HOST+result.getAuthor().getAvatar(),VolleyTool.getImageLoader());
            holder.avtar.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();
                    intent.setClass(mContext, TaPageActivity.class);
                    intent.putExtra("uid",result.getAuthor().getId());
                    intent.putExtra("username",result.getAuthor().getNkname());
                    mContext.startActivity(intent);
                }
            });
            holder.name.setText(result.getAuthor().getNkname());
            holder.time.setText(result.getCreated());
            holder.gridView.setAdapter(new ImageGridViewAdapter(mContext, result.getAttachs()));
            holder.content.setText(result.getTitle());
            //TODO 回去测试
            int type = -1;
            if(result.getCategory().getId() == 5){
                type = R.mipmap.icon_qa;
            }else{
                type = R.mipmap.icon_show;
            }
            int genderT = R.mipmap.boy_icon;
            if(result.getAuthor().getGender() == 1){
                genderT = R.mipmap.boy_icon;
            }else{
                genderT = R.mipmap.girl_icon;
            }
            int liked = result.getFavored();
            if(liked == 0){
                display(0,holder.shoucanged,holder.shouchangpre);
            }else{
                display(1, holder.shoucanged, holder.shouchangpre);
            }
            holder.shoucanged.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    likeOrUnlike(result.getId(),0,holder.shoucanged,holder.shouchangpre);
                }
            });
            holder.shouchangpre.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    likeOrUnlike(result.getId(),1,holder.shoucanged,holder.shouchangpre);
                }
            });
            holder.gender.setImageResource(genderT);
            holder.typeIcon.setImageResource(type);
            showComments(holder, result.getDigestComments());
            holder.gridView.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if(event.getAction() == MotionEvent.ACTION_UP){
                        Intent intent = new Intent();
                        intent.setClass(mContext, TopicDetailActivity.class);
                        intent.putExtra("id",result.getId());
                        mContext.startActivity(intent);
                        return false;
                    }
                    return false;
                }
            });
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();
                    intent.setClass(mContext, TopicDetailActivity.class);
                    intent.putExtra("id",result.getId());
                    mContext.startActivity(intent);
                }
            });
        }else if(viewHolder instanceof FootViewHolder){
            FootViewHolder footHolder = (FootViewHolder)viewHolder;
            footHolder.itemView.setTag("footView");
            footHolder.itemView.setVisibility(View.GONE);
        }
    }

    private void likeOrUnlike(int id,int i,ImageView shoucanged,ImageView shouchangpre){
        UserBean.User user = UserTools.getUser(mContext);
        if(user == null) {
            Intent intent1 = new Intent();
            intent1.setClass(mContext, LoginActivity.class);
            mContext.startActivity(intent1);
        }else{
            Map<String,String> params = new HashMap<>();
            params.put("id",id+"");
            params.put("acc_token",user.getAcc_token());
            String url = "";
            if(i == 0){
                url = Constants.UNFAVOR_TOPIC_URL;
            }else{
                url = Constants.FAVOR_TOPIC_URL;
            }
            display(i,shoucanged,shouchangpre);
            VolleyTool.get((Activity)mContext,url,params,null,1,null);
        }
    }

    private void display(int i,ImageView shoucanged,ImageView shouchangpre){
        if(i == 0){
            shoucanged.setVisibility(View.GONE);
            shouchangpre.setVisibility(View.VISIBLE);
        }else{
            shoucanged.setVisibility(View.VISIBLE);
            shouchangpre.setVisibility(View.GONE);
        }
    }

    private static final String JOIN_COUNT = "人参与了此问答";
    private void showComments(TopicHolder holder,ArrayList<TopicBean.DigestComment> comments){
        int counts = comments.size();
        String comment = "";
        if(counts == 0){
            holder.commentLayout.setVisibility(View.GONE);
        }else if(counts == 1){
            comment = comments.get(0).getContent();
            showLayout1(holder,comments);
            holder.commentLayout.setVisibility(View.VISIBLE);
            holder.avatorLayout2.setVisibility(View.GONE);
            holder.avatorLayout3.setVisibility(View.GONE);
        }else if(counts == 2){
            holder.commentLayout.setVisibility(View.VISIBLE);
            comment = comments.get(0).getContent();
            showLayout1(holder,comments);
            showLayout2(holder,comments);
            holder.avatorLayout3.setVisibility(View.GONE);
        }else if(counts == 3){
            holder.commentLayout.setVisibility(View.VISIBLE);
            comment = comments.get(0).getContent();
            showLayout1(holder,comments);
            showLayout2(holder,comments);
            showLayout3(holder,comments);
        }
        holder.comment.setText(comment);
        if(counts > 0){
            holder.joinCount.setVisibility(View.VISIBLE);
            holder.joinCount.setText(counts+JOIN_COUNT);
        }else{
            holder.joinCount.setVisibility(View.GONE);
        }

    }

    private void showLayout1(TopicHolder holder,final ArrayList<TopicBean.DigestComment> comments){
        holder.avatorLayout1.setVisibility(View.VISIBLE);
        holder.avatorLayout1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(mContext, TaPageActivity.class);
                intent.putExtra("uid", comments.get(0).getAuthor().getId());
                intent.putExtra("username", comments.get(0).getAuthor().getNkname());
                mContext.startActivity(intent);
            }
        });
        holder.avtar1.setDefaultImageResId(R.mipmap.empty_photo);
        holder.avtar1.setImageUrl(Constants.IMAGE_HOST+comments.get(0).getAuthor().getAvatar(),VolleyTool.getImageLoader());
    }

    private void showLayout2(TopicHolder holder,final ArrayList<TopicBean.DigestComment> comments){
        holder.avatorLayout2.setVisibility(View.VISIBLE);
        holder.avtar2.setDefaultImageResId(R.mipmap.empty_photo);
        holder.avatorLayout2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(mContext, TaPageActivity.class);
                intent.putExtra("uid",comments.get(1).getAuthor().getId());
                intent.putExtra("username",comments.get(1).getAuthor().getNkname());
                mContext.startActivity(intent);
            }
        });
        holder.avtar2.setImageUrl(Constants.IMAGE_HOST+comments.get(1).getAuthor().getAvatar(),VolleyTool.getImageLoader());
    }

    private void showLayout3(TopicHolder holder,final ArrayList<TopicBean.DigestComment> comments){
        holder.avatorLayout3.setVisibility(View.VISIBLE);
        holder.avatorLayout3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(mContext, TaPageActivity.class);
                intent.putExtra("uid",comments.get(2).getAuthor().getId());
                intent.putExtra("username",comments.get(2).getAuthor().getNkname());
                mContext.startActivity(intent);
            }
        });
        holder.avtar3.setDefaultImageResId(R.mipmap.empty_photo);
        holder.avtar3.setImageUrl(Constants.IMAGE_HOST+comments.get(2).getAuthor().getAvatar(),VolleyTool.getImageLoader());
    }

    @Override
    public int getItemViewType(int position) {
        if(position+1 == getItemCount()){
            return TYPE_FOOTER;
        }else{
            return TYPE_TOPIC;
        }
    }

    @Override
    public int getItemCount() {
        return results.size()+1;
    }

    class TopicHolder extends RecyclerView.ViewHolder{

        NetworkImageView avtar;
        TextView name;
        TextView time;
        NoScrollGridView gridView;
        RelativeLayout avatorLayout1;
        RelativeLayout avatorLayout2;
        RelativeLayout avatorLayout3;
        NetworkImageView avtar1;
        NetworkImageView avtar2;
        NetworkImageView avtar3;
        TextView joinCount;
        TextView comment;
        TextView content;
        ImageView typeIcon;
        LinearLayout commentLayout;
        ImageView gender;
        ImageView shoucanged;
        ImageView shouchangpre;
        ImageView ivComment;

        public TopicHolder(View itemView) {
            super(itemView);
            gender = (ImageView)itemView.findViewById(R.id.gender);
            shoucanged = (ImageView)itemView.findViewById(R.id.shoucanged);
            shouchangpre = (ImageView)itemView.findViewById(R.id.shoucangpre);
            ivComment = (ImageView)itemView.findViewById(R.id.ivcomment);
            avtar = (NetworkImageView)itemView.findViewById(R.id.avtar);
            name = (TextView)itemView.findViewById(R.id.name);
            time = (TextView)itemView.findViewById(R.id.time);
            gridView = (NoScrollGridView)itemView.findViewById(R.id.gridview);
            avatorLayout1 = (RelativeLayout)itemView.findViewById(R.id.avtarLayout1);
            avatorLayout2 = (RelativeLayout)itemView.findViewById(R.id.avtarLayout2);
            avatorLayout3 = (RelativeLayout)itemView.findViewById(R.id.avtarLayout3);
            avtar1 = (NetworkImageView)itemView.findViewById(R.id.avtar1);
            avtar2 = (NetworkImageView)itemView.findViewById(R.id.avtar2);
            avtar3 = (NetworkImageView)itemView.findViewById(R.id.avtar3);
            joinCount = (TextView)itemView.findViewById(R.id.joinCount);
            comment = (TextView)itemView.findViewById(R.id.comment);
            content = (TextView)itemView.findViewById(R.id.content);
            typeIcon = (ImageView)itemView.findViewById(R.id.typeIcon);
            commentLayout = (LinearLayout)itemView.findViewById(R.id.commentLayout);
        }
    }

    class FootViewHolder extends RecyclerView.ViewHolder{

        public FootViewHolder(View itemView) {
            super(itemView);
        }

    }

}
